package com.dr.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.ModelAndViewDefiningException;

import com.dr.pojo.User;
import com.dr.service.LoginService;

@Controller
public class LoginController {
	@Autowired
	private LoginService service;
	
//	@RequestMapping("/login")
//	public ModelAndView login(User user, HttpSession session) {
//		ModelAndView mav = new ModelAndView();
//		User loginUser = service.findUser(user);
//		if (loginUser == null) {
//			//登录失败
//			mav.addObject("loginErrMsg", "用户名或密码错误，请重新输入！");
//			mav.setViewName("login.jsp");
//		}else {
//			session.setAttribute("LoginUser", loginUser); //登录用户记录Session
//			//mav.setViewName("userManage/userList.jsp");
//			mav.setViewName("forward:/userList");
//		}		
//		return mav;
//	}
	
	@RequestMapping("/login")
	public String login(User user, HttpSession session) {		
		String path = "";
		User loginUser = service.findUser(user);
		if (loginUser == null) {
			//登录失败
			path = "login.jsp?loginErrMsg=用户名或密码错误，请重新输入！";  //页面
		}else {
			session.setAttribute("LoginUser", loginUser); //登录用户记录Session
			path = "forward:/um/userList"; 
		}		
		return path;
	}
}
